from django import forms
from django.forms import ModelForm
from reviews.models import DishImage, Dish
import re
from django.contrib.auth.models import User

RATING_CHOICES = (
    ('1', '1 Horrible'),
    ('2', '2 Poor'),
    ('3', '3 Just ok'),
    ('4', '4 Good'),
    ('5', '5 Excellent'),
)

class BulkAddPlacesForm(forms.Form):
    file = forms.FileField()

class SearchForm(forms.Form):
    query = forms.CharField(initial='What kind of food?', max_length=50, widget=forms.TextInput(attrs={'class':'shadow rounded'}))
    location = forms.CharField(initial='Where to look?', max_length=50, widget=forms.TextInput(attrs={'class':'shadow rounded'}))

class UploadDishPhotoForm(ModelForm):
    class Meta:
        model = DishImage
        exclude = ('dish', 'user',)

class EditTagsForm(forms.Form):
    dish_id = forms.IntegerField(widget=forms.HiddenInput, required=True)
    tags = forms.CharField(max_length=100, required=False)

# this is currently used for adding new dish AND adding a review...
class ReviewForm(forms.Form):
    name = forms.CharField()
    place = forms.CharField()
    tags = forms.CharField(required=False)
    text = forms.CharField(widget=forms.Textarea, required=False)
    rating = forms.ChoiceField(choices=RATING_CHOICES)
    review_id = forms.IntegerField(widget=forms.HiddenInput, required=False)


class AddDishForm(ModelForm):
    class Meta:
        model = Dish
        fields = ('name', 'tags')

class QuickReviewForm(forms.Form):
    text = forms.CharField(widget=forms.Textarea(attrs={'class':'quick_review'}), required=True, initial='Write stuff here')

class RegistrationForm(forms.Form):
    username = forms.CharField(label=u'Username', max_length=30)
    password1 = forms.CharField(
        label=u'Password',
        widget=forms.PasswordInput()
    )
    email = forms.EmailField(label=u'Email')
    
    def clean_username(self):
        username = self.cleaned_data['username']
        if not re.search(r'^\w+$', username):
            raise forms.ValdiationError('Username can only contain '
                'alphanumeric characters and the underscore.')
        try:
            User.objects.get(username=username)
        except User.DoesNotExist:
            return username
        raise forms.ValidationError('Username is already taken.')

